import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginApi } from '@/apis/login'
import type { LoginRes } from '@/types/user'

const userUserStore = defineStore(
  'user',
  () => {
    // as 非空断言
    //userinfo-存储用户信息
    const userinfo = ref({} as LoginRes)

    //登录
    const login = async (data: any) => {
      const res = await loginApi(data)
      userinfo.value = res.data.data
      //   console.log(userinfo.value)
    }

    //退出
    const logout = () => {
      userinfo.value = {} as LoginRes
    }
    return {
      userinfo,
      login,
      logout,
    }
  },
  {
    persist: true,
    //让数据持久化存储
  },
)
export default userUserStore
